const { search, insert, update, del, findOne } = require('../model/tag.model');

class TagService {
  // 查询标签列表
  async searchTag({ pageNum, pageSize }) {
    const page = (pageNum-1) * pageSize;

    // 分页
    let whereLimit = `limit ${page}, ${pageSize}`;
    
    const sql = `select * from b_tag ${whereLimit}`;
    const res = await search(sql)
    return res
  }
  // 查询标签列表总数
  async searchTagTotal() {
   
    const sql = `select count(*) as total from b_tag`;
    const res = await search(sql)
    return res
  }
  // 添加标签
  async insertTag({tag_name}) {
    const params = { tag_name }
    const res = await insert('b_tag', params)
    return res
  }
  // 修改标签
  async updateTag({id, tag_name}) {
    const params = {}
    const whereOpt = {}

    id && Object.assign(whereOpt, { id })

    tag_name && Object.assign(params, { tag_name })
  
    const res = await update('b_tag', params, whereOpt)
    return res
  }
  // 删除标签
  async deleteTag({id}) {
    const params = {}

    id && Object.assign(params, { id })
    const res = await del('b_tag', params)
    return res
  }

  // 查询某一个标签
  async findTag({id, tag_name}) {
    const params = {}

    id && Object.assign(params, { id })
    tag_name && Object.assign(params, { tag_name })
    
    const res = await findOne('b_tag', params)
    return res.length > 0 ? res : null
  }
}

module.exports = new TagService();
